package org.fujay.langchain4jserver.domain.application.vo;

import java.io.Serial;
import java.io.Serializable;

/**
 * 应用用量指标值对象
 */
public record UsageMetrics(
        int apiCallCount, long llmPromptTokens, long llmCompletionTokens, long embeddingTokens)
        implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    public static UsageMetrics zero() {
        return new UsageMetrics(0, 0L, 0L, 0L);
    }

    public void validate() {
        if (apiCallCount < 0 || llmPromptTokens < 0 || llmCompletionTokens < 0 || embeddingTokens < 0) {
            throw new IllegalArgumentException("Usage metrics cannot be negative.");
        }
    }

    public UsageMetrics add(UsageMetrics other) {
        return new UsageMetrics(
                this.apiCallCount + other.apiCallCount,
                this.llmPromptTokens + other.llmPromptTokens,
                this.llmCompletionTokens + other.llmCompletionTokens,
                this.embeddingTokens + other.embeddingTokens);
    }
}
